package persistence.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import org.apache.log4j.Logger;

import persistence.dao.RoutsStantionDao;
import persistence.entity.RoutsStantionEntity;

public class RoutsStantionDaoImpl implements RoutsStantionDao {
	
	private static final Logger logger = Logger.getLogger(RoutsStantionDaoImpl.class);
	
	private static final String ROUTSSTANTIONDAOIMPL_INFO = "Could not getRoutsStantion"; 
	private static final String ROUTSSTANTIONDAOIMPL_INFO_INSERT = "Invalid Insert RoutsStantion"; 
	private static final String ROUTSSTANTIONDAOIMPL_INFO_DELETE = "Invalid Delete RoutsStantion"; 
	
	private static final String INSERT_ROUTSSTANTION = "INSERT INTO `route_stantion`(id_routs, id_city) VALUES (?,?)";
	
	private static final String DELETE_ID ="DELETE from `route_stantion` WHERE id=?";
	
	private static final String DELETE_ID_ROUTS ="DELETE from `route_stantion` WHERE id_routs=?";
	
	private Connection connection;
	public RoutsStantionDaoImpl(Connection conn){
		this.connection = conn;
	}
	@Override
	public Class<RoutsStantionEntity> getEntityClass() {
		return null;
	}
	
	@Override
	public List<RoutsStantionEntity> findAll() {
		return null;
	}
	@Override
	public List<RoutsStantionEntity> findByExample(
			RoutsStantionEntity exampleInstance) {
		return null;
	}
	@Override
	public int countAll() {
		return 0;
	}
	@Override
	public int countByExample(RoutsStantionEntity exampleInstance) {
		return 0;
	}
	@Override
	public RoutsStantionEntity insert(RoutsStantionEntity entity) {
		return null;
	}
	@Override
	public RoutsStantionEntity update(RoutsStantionEntity entity) {
		return null;
	}
			
	@Override
	public RoutsStantionEntity setRoutsStantion(int[] id_city, int id_routs) {
				
		PreparedStatement stmt = null;
		ResultSet rs = null;

		try {
			if (id_city !=null && id_routs >0 )
			{
				for (int i=0; i<id_city.length; i++){
					stmt = connection.prepareStatement(RoutsStantionDaoImpl.INSERT_ROUTSSTANTION);
					stmt.setInt(1, id_routs);
					stmt.setInt(2, id_city[i]);
					stmt.executeUpdate();
				}
			}else{
				logger.error(RoutsStantionDaoImpl.ROUTSSTANTIONDAOIMPL_INFO_INSERT );
			}
		} catch (Exception e) {
			logger.error(RoutsStantionDaoImpl.ROUTSSTANTIONDAOIMPL_INFO + e.getMessage());
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (Exception ignore) {
				}
			}
			if (stmt != null) {
				try {
					stmt.close();
				} catch (Exception ignore) {
				}
			}
		}
		return null;
	}
	@Override
	public RoutsStantionEntity delete(int[] id_stantion_city) {
		PreparedStatement stmt = null;
		ResultSet rs = null;

		try {
			if (id_stantion_city != null)
			{	
				for(int i=0; i<id_stantion_city.length;i++){
					stmt = connection.prepareStatement(RoutsStantionDaoImpl.DELETE_ID);
					stmt.setInt(1, id_stantion_city[i]);
					stmt.executeUpdate();
				}
			}else{
				logger.error(RoutsStantionDaoImpl.ROUTSSTANTIONDAOIMPL_INFO_DELETE);
			}
		} catch (Exception e) {
			logger.error(RoutsStantionDaoImpl.ROUTSSTANTIONDAOIMPL_INFO + e.getMessage());
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (Exception ignore) {
				}
			}
			if (stmt != null) {
				try {
					stmt.close();
				} catch (Exception ignore) {
				}
			}
		}
		return null;
	}
	@Override
	public RoutsStantionEntity deleteOrder(int[] id_check_routs) {
		PreparedStatement stmt = null;
		ResultSet rs = null;

		try {
			if (id_check_routs != null)
			{	
				for(int i=0; i<id_check_routs.length;i++){
					stmt = connection.prepareStatement(RoutsStantionDaoImpl.DELETE_ID_ROUTS);
					stmt.setInt(1, id_check_routs[i]);
					stmt.executeUpdate();
				}
			}else{
				logger.error(RoutsStantionDaoImpl.ROUTSSTANTIONDAOIMPL_INFO_DELETE);
			}
		} catch (Exception e) {
			logger.error(RoutsStantionDaoImpl.ROUTSSTANTIONDAOIMPL_INFO + e.getMessage());
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (Exception ignore) {
				}
			}
			if (stmt != null) {
				try {
					stmt.close();
				} catch (Exception ignore) {
				}
			}
		}
		return null;
	}
}
